<template>
  <Dialog v-model="visible" title="硬件配置" width="600">
    <el-divider content-position="left">果丁设备-Lora</el-divider>
    <el-descriptions border :column="1">
      <el-descriptions-item label="应用ID">
        <el-input v-model="gd.appId"/>
      </el-descriptions-item>
      <el-descriptions-item label="应用Secret">
        <el-input v-model="gd.appSecret"/>
      </el-descriptions-item>
    </el-descriptions>

    <el-divider content-position="left">果丁设备-ZWave</el-divider>
    <el-descriptions border :column="1">
      <el-descriptions-item label="应用ID">
        <el-input v-model="gdZwave.appId"/>
      </el-descriptions-item>
      <el-descriptions-item label="应用Secret">
        <el-input v-model="gdZwave.appSecret"/>
      </el-descriptions-item>
    </el-descriptions>

    <el-divider content-position="left">新易安设备</el-divider>
    <el-descriptions border :column="1">
      <el-descriptions-item label="appId">
        <el-input v-model="ya.appId"/>
      </el-descriptions-item>
      <el-descriptions-item label="developId">
        <el-input v-model="ya.developId"/>
      </el-descriptions-item>
      <el-descriptions-item label="appSecret">
        <el-input v-model="ya.appSecret"/>
      </el-descriptions-item>

      <el-descriptions-item label="产品ID">
        <el-input v-model="ya.pid"/>
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <el-button :disabled="loading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>.

<script lang="ts" setup>
import {useSchoolApi} from "@/api/yxt/school";

const schoolApi = useSchoolApi();

const visible = ref<boolean>(false)
const loading = ref<boolean>(false)
const schoolId = ref<number>(undefined)
const gd = ref<any>({
  appId: "",
  appSecret: "",
})
const gdZwave = ref<any>({
  appId: "",
  appSecret: "",
})
const ya = ref<any>({
  appId: "",
  appSecret: "",
  developId: "",
  pid: "",
})

const open = async (id?: number) => {
  visible.value = true
  resetForm()
  schoolId.value = id;
  if (id) {
    loading.value = true
    try {
      const config = await schoolApi.getSupplierConfig(id);
      gd.value = config.gd;
      ya.value = config.ya;
      gdZwave.value = config.gdZwave;
    } finally {
      loading.value = false
    }
  }
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = async () => {
  try {
    await schoolApi.updateSupplierConfig({
      id: schoolId.value,
      gd: gd.value,
      ya: ya.value,
      gdZwave: gdZwave.value
    });
    visible.value = false
    emit('success')
  } finally {
    loading.value = false
  }
}

const resetForm = () => {
  schoolId.value = undefined;
  gd.value = {
    appId: "",
    appSecret: "",
  }
  ya.value = {
    appId: "",
    appSecret: "",
    developId: "",
    pid: "",
  }
  gdZwave.value = {
    appId: "",
    appSecret: ""
  }
}
</script>
